https://laihao.vaserver.com/hairdressingusers
要來講解美髮預約系統_users的程式碼
就是MVC架構
各畫面的程式碼解說
顯示出資料內容的程式碼
/home/laihao/public_html/resources/views/hairdressing/users/index.blade.php
程式碼
<!DOCTYPE html>
<html lang="zh-Hant-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>使用者管理</title>
<style>
body { font-family: 'Microsoft JhengHei', sans-serif; background: #f9f3ff; margin: 0; padding: 0; }
h1 { text-align: center; color: #6a0dad; margin-top: 30px; margin-bottom: 25px; }
.btn { display: inline-block; background: #6a0dad; color: white; text-align: center;
padding: 8px 20px; border-radius: 25px; text-decoration: none; border: none;
cursor: pointer; font-size: 14px; }
.btn:hover { background: #530a9e; }
.btn-edit { background: #f0ad4e; }
.btn-edit:hover { background: #d99332; }
.btn-delete { background: #d9534f; }
.btn-delete:hover { background: #b52b27; }
.top-bar { width: 90%; margin: 0 auto 20px; display: flex; justify-content: flex-end; align-items: center; }
.success { text-align: center; color: green; font-weight: bold; }
table { width: 90%; margin: 0 auto; border-collapse: collapse; background: white;
border-radius: 10px; overflow: hidden; box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
th { background: #6a0dad; color: white; padding: 12px; white-space: nowrap; }
td { padding: 12px; border-bottom: 1px solid #eee; text-align: center; }
tr:hover { background: #f3e8ff; }
.role-admin { color: #6a0dad; font-weight: bold; }
.role-receptionist { color: #4a90d9; font-weight: bold; }
.role-stylist { color: #f0ad4e; font-weight: bold; }
.status-active { color: #5cb85c; font-weight: bold; }
.status-inactive { color: #999; }
.actions { white-space: nowrap; }
.actions form { display: inline-block; margin: 0; }
.actions .btn { margin: 2px; }
</style>
</head>
<body>
<h1>🔑 使用者管理</h1>
<div class="top-bar">
<a href="{{ route('hairdressingusers.create') }}" class="btn">+ 新增使用者</a>
</div>
@if(session('success'))
<p class="success">{{ session('success') }}</p>
@endif
<table>
<thead>
<tr>
<th>姓名</th>
<th>Email</th>
<th>角色</th>
<th>狀態</th>
<th>最後登入</th>
<th>操作</th>
</tr>
</thead>
<tbody>
@foreach($hairdressingusers as $hairdressinguser)
<tr>
<td>{{ $hairdressinguser->name }}</td>
<td>{{ $hairdressinguser->email }}</td>
<td>
@if($hairdressinguser->role === 'admin')
<span class="role-admin">管理員</span>
@elseif($hairdressinguser->role === 'receptionist')
<span class="role-receptionist">櫃台</span>
@else
<span class="role-stylist">設計師</span>
@endif
</td>
<td>
@if($hairdressinguser->status === 'active')
<span class="status-active">啟用</span>
@else
<span class="status-inactive">停用</span>
@endif
</td>
<td>{{ $hairdressinguser->last_login_at ?? '-' }}</td>
<td class="actions">
<a href="{{ route('hairdressingusers.edit', $hairdressinguser->id) }}" class="btn btn-edit">編輯</a>
<form action="{{ route('hairdressingusers.destroy', $hairdressinguser->id) }}" method="POST"
onsubmit="return confirm('確定要刪除這位使用者嗎?');">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-delete">刪除</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
這是一個 Laravel Blade 的「使用者管理」列表頁面。它從控制器接收 $hairdressingusers 集合,用表格列出所有使用者,並提供新增、編輯、刪除功能,以及操作後的快閃成功訊息。
<!DOCTYPE html>
<html lang="zh-Hant-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>使用者管理</title>
<!DOCTYPE html>:宣告 HTML5。lang="zh-Hant-TW":頁面語言為台灣繁體中文。charset="UTF-8":支援中文與 emoji(例如 🔑)。viewport:讓頁面在手機上能依螢幕寬度縮放,提升 RWD 效果。<title>:瀏覽器分頁顯示「使用者管理」。body {
font-family: 'Microsoft JhengHei', sans-serif;
background: #f9f3ff;
margin: 0;
padding: 0;
}
.top-bar {
width: 90%;
margin: 0 auto 20px;
display: flex;
justify-content: flex-end;
align-items: center;
}
justify-content: flex-end 把「+ 新增使用者」按鈕推到右側。.success {
text-align: center;
color: green;
font-weight: bold;
}
table {
width: 90%;
margin: 0 auto;
border-collapse: collapse;
background: white;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
overflow: hidden 讓 border-radius 在表格上正常顯示。th {
background: #6a0dad;
color: white;
padding: 12px;
white-space: nowrap;
}
white-space: nowrap 避免表頭文字換行,保持整齊。tr:hover {
background: #f3e8ff;
}
.role-admin { color: #6a0dad; font-weight: bold; }
.role-receptionist { color: #4a90d9; font-weight: bold; }
.role-stylist { color: #f0ad4e; font-weight: bold; }
.status-active { color: #5cb85c; font-weight: bold; }
.status-inactive { color: #999; }
.btn {
display: inline-block;
background: #6a0dad;
color: white;
text-align: center;
padding: 8px 20px;
border-radius: 25px;
text-decoration: none;
border: none;
cursor: pointer;
font-size: 14px;
}
.btn:hover { background: #530a9e; }
.btn-edit { background: #f0ad4e; }
.btn-edit:hover { background: #d99332; }
.btn-delete { background: #d9534f; }
.btn-delete:hover { background: #b52b27; }
.btn:預設紫色按鈕。.btn-edit:編輯按鈕為橘色。.btn-delete:刪除按鈕為紅色。.actions { white-space: nowrap; }
.actions form { display: inline-block; margin: 0; }
.actions .btn { margin: 2px; }
white-space: nowrap:避免「編輯」「刪除」按鈕換行。<form> 中,設為 inline-block,讓它跟編輯按鈕排在同一列。<h1>🔑 使用者管理</h1>
<div class="top-bar">
<a href="{{ route('hairdressingusers.create') }}" class="btn">+ 新增使用者</a>
</div>
<h1>:頁面主標題。.top-bar 中的連結:
route('hairdressingusers.create'):利用命名路由產生「新增使用者」頁面網址。@if(session('success'))
<p class="success">{{ session('success') }}</p>
@endif
session('success'):從控制器帶過來的快閃訊息,例如:
return redirect()->route('hairdressingusers.index')
->with('success', '使用者新增成功!');
<table>
<thead>
<tr>
<th>姓名</th>
<th>Email</th>
<th>角色</th>
<th>狀態</th>
<th>最後登入</th>
<th>操作</th>
</tr>
</thead>
<tbody>
...
</tbody>
</table>
<tbody> 中用 @foreach 跑迴圈產生。@foreach($hairdressingusers as $hairdressinguser)
<tr>
<td>{{ $hairdressinguser->name }}</td>
<td>{{ $hairdressinguser->email }}</td>
...
</tr>
@endforeach
$hairdressingusers 是從控制器傳來的集合(通常是 HairdressingUser 模型的查詢結果)。$hairdressinguser 代表一筆使用者資料。<td>{{ $hairdressinguser->name }}</td>
<td>{{ $hairdressinguser->email }}</td>
<td>
@if($hairdressinguser->role === 'admin')
<span class="role-admin">管理員</span>
@elseif($hairdressinguser->role === 'receptionist')
<span class="role-receptionist">櫃台</span>
@else
<span class="role-stylist">設計師</span>
@endif
</td>
role 欄位(admin、receptionist、stylist)顯示不同中文與顏色。<td>
@if($hairdressinguser->status === 'active')
<span class="status-active">啟用</span>
@else
<span class="status-inactive">停用</span>
@endif
</td>
active → 綠色「啟用」。inactive)→ 灰色「停用」。<td>{{ $hairdressinguser->last_login_at ?? '-' }}</td>
last_login_at 有值,就顯示時間字串。null,顯示 - 表示尚未登入。?? 運算子(null 合併運算子)。<td class="actions">
<a href="{{ route('hairdressingusers.edit', $hairdressinguser->id) }}"
class="btn btn-edit">編輯</a>
<form action="{{ route('hairdressingusers.destroy', $hairdressinguser->id) }}"
method="POST"
onsubmit="return confirm('確定要刪除這位使用者嗎?');">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-delete">刪除</button>
</form>
</td>
route('hairdressingusers.edit', $hairdressinguser->id):
/hairdressingusers/5/edit。edit($id) 方法。action="{{ route('hairdressingusers.destroy', $hairdressinguser->id) }}":
/hairdressingusers/5。destroy($id) 方法。method="POST":HTML 表單只能用 GET/POST,這裡用 POST 再搭配 Laravel 的假方法。@csrf:加入 CSRF Token,防止跨站請求。@method('DELETE'):讓 Laravel 把這個請求視為 DELETE。onsubmit="return confirm('確定要刪除這位使用者嗎?');":
$hairdressingusers 並傳入視圖。session('success')。session('success') 就顯示綠色成功訊息。